package com.heshida.service.impl;

import com.heshida.mapper.BusinessMapper;
import com.heshida.mapper.CartMapper;
import com.heshida.mapper.OrderDetailetMapper;
import com.heshida.mapper.OrdersMapper;
import com.heshida.po.Business;
import com.heshida.po.Cart;
import com.heshida.po.OrderDetailet;
import com.heshida.po.Orders;
import com.heshida.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderDetailetMapper orderDetailetMapper;

    @Transactional
    public int createOrders(String userId, Integer businessId, Integer daId) {

        int orderId = 0;


        //1、查询当前用户购物车中当前商家的所有食品
        Cart cart = new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        List<Cart> cartList = cartMapper.listCart(cart);
        //2、查询商家信息（需要使用商家的配送费信息）
        Business business = businessMapper.getBusinessById(businessId);
        //3、获取订单总额
        Double ordersTotal = 0.0;  //订单总额
        for (Cart c : cartList) {
            //累计所有食品总价格
            ordersTotal += c.getFood().getFoodPrice() * c.getQuantity();
        }
        //加上配送费
        ordersTotal += business.getDeliveryPrice();
        //3、创建订单，并获取订单编号
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setOrderTotal(ordersTotal);
        orders.setDaId(daId);
        //保存订单
        ordersMapper.saveOrders(orders);
        orderId=orders.getOrderId();
        //4、处理相关数据：获取订单明细集合
        List<OrderDetailet> orderDetailetList = new ArrayList();  //订单明细集合
        for (Cart c : cartList) {
            OrderDetailet od = new OrderDetailet();
            od.setOrderId(orderId);
            od.setFoodId(c.getFoodId());
            od.setQuantity(c.getQuantity());
            orderDetailetList.add(od);
        }
        //5、批量添加订单明细
        //if(!(orderDetailetList.isEmpty())){

        orderDetailetMapper.saveOrderDetailetBatch(orderDetailetList);
        System.out.println("订单明细保存成功success.....");
        //  //保存订单明细
        // }
        //6、清空当前用户购物车中当前商家的所有食品
        cartMapper.removeCart(cart);
        return orderId;
    }


    public Orders getOrdersById(Integer orderId) {

        Orders orders = null;


        try {

            orders = ordersMapper.getOrdersById(orderId);
            List<OrderDetailet> list = orderDetailetMapper.listOrderDetailetByOrderId(orderId);
            orders.setOdList(list);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    public List<Orders> listOrderByUserId(String userId) {
        List<Orders> list= new ArrayList();
        try {
            list=ordersMapper.listOrderByUserId(userId);
            for(Orders o:list){
                List<OrderDetailet> odList=orderDetailetMapper.listOrderDetailetByOrderId(o.getOrderId());
                o.setOdList(odList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public int updateByOrderId(int orderId) {
        return ordersMapper.updateByOrderId(orderId);
    }
}
